package com.wgchao.authmanager.application.controller;

import com.wgchao.authmanager.application.filter.AuthMethod;
import com.wgchao.authmanager.application.filter.Authentication;
import com.wgchao.authmanager.application.service.AgencyService;
import com.wgchao.authmanager.application.vo.AgencyVo;
import com.wgchao.authmanager.domain.params.AgencyCreator;
import com.wgchao.authmanager.domain.params.AgencyUpdator;
import com.wgchao.authmanager.infrastructure.resp.BackResult;
import com.wgchao.authmanager.query.AgencyInfoQuery;
import com.wgchao.authmanager.query.specification.AgencyCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: AgencyManagerController
 * @author: wgchao
 * @createTime: 2020/4/29 5:00 PM
 * @Description: 机构管理
 */
@RestController
@RequestMapping("/aum/front/agency")
@Api(value = "/aum/front/agency", description = "机构管理")
public class AgencyManagerController {
    @Autowired
    private AgencyService agencyService;
    @Autowired
    private AgencyInfoQuery agencyInfoQuery;


    @PostMapping
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "新建机构", notes = "需要身份验证")
    public BackResult createAgency(@ApiParam(value = "机构信息", name = "agencyCreator", required = true)
                                       @RequestBody AgencyCreator agencyCreator){
        agencyService.addNewAgency(agencyCreator, "admin");
        return BackResult.success();
    }

    @PutMapping("/{agencyCode}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "修改机构信息", notes = "需要身份验证")
    public BackResult resiveAgencyInfo(@ApiParam(value = "机构编号（url参数）", name = "agencyCode", required = true)
                                           @PathVariable String agencyCode,
                                       @ApiParam(value = "机构修改信息", name = "agencyUpdator", required = true)
                                       @RequestBody AgencyUpdator agencyUpdator){
        agencyService.reviseAgencyInfo(agencyCode, agencyUpdator, "admin");
        return BackResult.success();
    }

    /**
     * 删除机构
     * @param agencyCode
     * @return
     */
    @DeleteMapping("/{agencyCode}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "删除机构", notes = "需要身份验证")
    public BackResult delAgency(@ApiParam(value = "机构编号（url参数）", name = "agencyCode", required = true)
                                    @PathVariable String agencyCode){
        agencyService.removeAgency(agencyCode, "admin");
        return BackResult.success();
    }

    /**
     * 停用机构
     * @param agencyCode
     * @return
     */
    @PutMapping("/stop/{agencyCode}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "停用机构", notes = "需要身份验证")
    public BackResult stopAgency(@ApiParam(value = "机构编号（url参数）", name = "agencyCode", required = true)
                                     @PathVariable String agencyCode){
        agencyService.stopUsingAgency(agencyCode, "admin");
        return BackResult.success();
    }

    /**
     * 重新启用机构
     * @param agencyCode
     * @return
     */
    @PutMapping("/reable/{agencyCode}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "重新启用机构", notes = "需要身份验证")
    public BackResult reableAgency(@ApiParam(value = "机构编号（url参数）", name = "agencyCode", required = true)
                                       @PathVariable String agencyCode){
        agencyService.reEnableAgency(agencyCode, "admin");
        return BackResult.success();
    }

    /**
     * 条件查询机构信息
     * @return
     */
    @GetMapping("/spec")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "条件查询机构信息", notes = "需要身份验证")
    public BackResult<List<AgencyVo>> searchAgencysOfPage(@ApiParam(value = "查询过滤条件", name = "agencySpec", required = true)
                                                                  AgencyCriteria agencyCriteria) throws Exception{
        List<AgencyVo> agencyVoList = agencyInfoQuery.searchAgencysOfSpec(agencyCriteria);
        return BackResult.success().setData(agencyVoList);
    }
}
